package org.ix.dynamicapis.service.impl;

import org.ix.dynamicapis.exception.DmpDataNotFoundException;
import org.ix.dynamicapis.model.PageList;
import org.ix.dynamicapis.utils.ObjectUtils;
import org.ix.dynamicapis.constant.DeletedConstant;
import org.ix.dynamicapis.presentation.condition.ApiSelectCondition;
import org.ix.dynamicapis.presentation.condition.ApiUpdateCondition;
import org.ix.dynamicapis.presentation.entity.ApiEntity;
import org.ix.dynamicapis.presentation.entity.ApiEntityExt;
import org.ix.dynamicapis.presentation.mapper.ApiMapper;
import org.ix.dynamicapis.model.request.ApiAddRequest;
import org.ix.dynamicapis.model.request.ApiPatchRequest;
import org.ix.dynamicapis.model.request.ApiQueryRequest;
import org.ix.dynamicapis.model.request.ApiSaveRequest;
import org.ix.dynamicapis.model.response.ApiDetailResponse;
import org.ix.dynamicapis.model.response.ApiListItemResponse;
import org.ix.dynamicapis.service.ApiService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 接口配置业务逻辑
 *
 * @author 郗世豪（rarexixi@gmail.com）
 */
@Service("apiService")
@Transactional
public class ApiServiceImpl implements ApiService {

    final ApiMapper apiMapper;

    @Autowired
    public ApiServiceImpl(ApiMapper apiMapper) {
        this.apiMapper = apiMapper;
    }

    /**
     * 添加接口配置
     *
     * @param addRequest 接口配置
     * @return 受影响的行数
     * @author 郗世豪（rarexixi@gmail.com）
     */
    @Override
    public ApiDetailResponse add(ApiAddRequest addRequest) {
        ApiEntity entity = ObjectUtils.copy(addRequest, ApiEntity.class);
        apiMapper.insert(entity);
        return getById(entity.getId());
    }

    /**
     * 批量添加接口配置
     *
     * @param list 接口配置列表
     * @return 受影响的行数
     * @author 郗世豪（rarexixi@gmail.com）
     */
    @Override
    public int batchAdd(Collection<ApiAddRequest> list) {
        List<ApiEntity> entityList = ObjectUtils.copy(list, ApiEntity.class);
        return apiMapper.batchInsert(entityList);
    }

    /**
     * 删除接口配置
     *
     * @param patchRequest 更新条件请求
     * @return 受影响的行数
     * @author 郗世豪（rarexixi@gmail.com）
     */
    @Override
    public int delete(ApiPatchRequest patchRequest) {
        ApiUpdateCondition condition = ObjectUtils.copy(patchRequest, ApiUpdateCondition.class);
        return apiMapper.delete(condition);
    }

    /**
     * 禁用接口配置
     *
     * @param patchRequest 更新条件请求
     * @return 受影响的行数
     * @author 郗世豪（rarexixi@gmail.com）
     */
    @Override
    public int disable(ApiPatchRequest patchRequest) {
        ApiUpdateCondition condition = ObjectUtils.copy(patchRequest, ApiUpdateCondition.class);
        ApiEntity entity = ObjectUtils.copy(patchRequest, ApiEntity.class, "id");
        entity.setDeleted(DeletedConstant.INVALID);
        return apiMapper.update(entity, condition);
    }

    /**
     * 启用接口配置
     *
     * @param patchRequest 更新条件请求
     * @return 受影响的行数
     * @author 郗世豪（rarexixi@gmail.com）
     */
    @Override
    public int enable(ApiPatchRequest patchRequest) {
        ApiUpdateCondition condition = ObjectUtils.copy(patchRequest, ApiUpdateCondition.class);
        ApiEntity entity = ObjectUtils.copy(patchRequest, ApiEntity.class, "id");
        entity.setDeleted(DeletedConstant.VALID);
        return apiMapper.update(entity, condition);
    }

    /**
     * 根据接口ID更新接口配置
     *
     * @param saveRequest 保存接口配置请求实体
     * @return 更新后的接口配置详情
     * @author 郗世豪（rarexixi@gmail.com）
     */
    @Override
    public ApiDetailResponse updateById(ApiSaveRequest saveRequest) {
        ApiUpdateCondition condition = new ApiUpdateCondition();
        condition.setId(saveRequest.getId());
        ApiEntity entity = ObjectUtils.copy(saveRequest, ApiEntity.class);
        apiMapper.update(entity, condition);
        return getById(saveRequest.getId());
    }

    /**
     * 根据接口ID获取接口配置详情
     *
     * @param id 接口ID
     * @return 接口配置详情
     * @author 郗世豪（rarexixi@gmail.com）
     */
    @Override
    @Transactional(readOnly = true)
    public ApiDetailResponse getById(Integer id) {
        ApiEntityExt entity = apiMapper.detail(id);
        if (entity == null) {
            throw new DmpDataNotFoundException("接口配置不存在");
        }
        return ObjectUtils.copy(entity, ApiDetailResponse.class);
    }

    /**
     * 获取接口配置列表
     *
     * @param queryRequest 搜索条件
     * @return 符合条件的接口配置列表
     */
    @Override
    @Transactional(readOnly = true)
    public List<ApiListItemResponse> getList(ApiQueryRequest queryRequest) {
        ApiSelectCondition condition = ObjectUtils.copy(queryRequest, ApiSelectCondition.class);
        List<ApiEntityExt> list = apiMapper.select(condition);
        return ObjectUtils.copy(list, ApiListItemResponse.class);
    }

    /**
     * 分页获取接口配置列表
     *
     * @param queryRequest 搜索条件
     * @param pageNum      页码
     * @param pageSize     分页大小
     * @return 符合条件的接口配置分页列表
     */
    @Override
    @Transactional(readOnly = true)
    public PageList<ApiListItemResponse> getPageList(ApiQueryRequest queryRequest, Integer pageNum, Integer pageSize) {

        ApiSelectCondition condition = ObjectUtils.copy(queryRequest, ApiSelectCondition.class);
        PageInfo<ApiEntityExt> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> apiMapper.select(condition));

        List<ApiListItemResponse> list = ObjectUtils.copy(pageInfo.getList(), ApiListItemResponse.class);
        return new PageList<>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), list);
    }
}
